package Controller;

import View.admin.AdminWindow;
import View.admin.RoomDialog;
import View.admin.UserDialog;
import agh.rozproszony.model.Room;
import agh.rozproszony.model.User;
import agh.rozproszony.reservations.ReservationsServer;
import java.rmi.RemoteException;

public class AdminController {

    protected ReservationsServer server;
    protected AdminWindow adminWindow;

    public AdminController(ReservationsServer server) {
        this.server = server;
        adminWindow = new AdminWindow(this);
    }

    public void show() {
        
        adminWindow.setVisible(true);
        refreshUsersInGUI();
        refreshRoomsInGUI();
    }

    public void deleteUser(User user) {
        try {
            System.out.println("KLIENT: usuwanie uzytkownika: " + user.getLogin());
            server.deleteUser(user);
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void refreshUsersInGUI() {
        try {
            adminWindow.refreshUsersTable(server.getAllUsers());
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void openAddUserDialog() {
        UserDialog userDialog = new UserDialog(this, adminWindow, true);
        userDialog.setVisible(true);
    }

    public void addUser(User user) {
        try {
            server.insertUser(user);
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public boolean userExists(String login) {
        try {
            if (server.getUserByLogin(login) != null) {
                return true;
            }
            return false;
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void deleteRoom(Room room) {
        try {
            System.out.println("KLIENT: usuwanie pokoju: " + room.getName());
            server.deleteRoom(room);
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void refreshRoomsInGUI() {
        try {
            adminWindow.refreshRoomsTable(server.getAllRooms());
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void openRoomAddDialog() {
        RoomDialog roomDialog = new RoomDialog(this, adminWindow, true);
        roomDialog.setVisible(true);
    }

    public void addRoom(Room room) {
        try {
            server.insertRoom(room);
        } catch (RemoteException ex) {
            throw new RuntimeException(ex);
        }
    }
}
